var base_url = 'http://localhost/sport/wchart/wchart/';

function ajaxDataRenderer(url) {
    var ret = null;
    $.ajax({
      // have to use synchronous here, else the function 
      // will return before the data is fetched
      async: false, cache: false, url: url, dataType:"json",
      success: function(data) {
      	if (data.success) {
	        ret = data;
      	}
      }
    });
    return ret;
};

function drawChart(div_name, chart_code, chart_title, data, ticks, labels) {
	ret = ajaxDataRenderer(base_url+'draw/'+chart_code);
	// Se i dati mi giungono dalla variabile arra_data allora sovrascrivo i dati presi dalle serie configurate sul db
	if (data==null) {
		if (ret.data.length==0)
			return null;
	} else
		ret.data=data;
		
	if (chart_title==null)
		chart_title=ret.title;
	switch(ret.type)
	{
		case 'L': //linear
			  $.jqplot(div_name, ret.data,{
			    title: chart_title
				, grid: {
					// borderColor:'transparent',drawBorder:false,shadowColor:'transparent',
				    drawGridLines: true,        // wether to draw lines across the grid or not.
				    gridLineColor: '#cccccc',   // *Color of the grid lines.
				    background: '#ffffff',      // CSS color spec for background color of grid.
				    borderColor: '#cccccc',     // CSS color spec for border around grid.
				    borderWidth: 1.0,           // pixel width of border around grid.
				    shadow: false,               // draw a shadow for grid.
				    shadowAngle: 45,            // angle of the shadow.  Clockwise from x axis.
				    shadowOffset: 1.5,          // offset from the line of the shadow.
				    shadowWidth: 3,             // width of the stroke for the shadow.
				    shadowDepth: 3,             // Number of strokes to make when drawing shadow.
				                                // Each stroke offset by shadowOffset from the last.
				    shadowAlpha: 0.07,          // Opacity of the shadow
				    renderer: $.jqplot.CanvasGridRenderer,  // renderer to use to draw the grid.
				    rendererOptions: {}         // options to pass to the renderer.  Note, the default
				                                // CanvasGridRenderer takes no additional options.
				}
				, axes:{yaxis:{min:0, max:50}}
			  });
		  break;
		case 'P': //pie
  			$.jqplot (div_name, ret.data,{
					    title: chart_title
      					,seriesDefaults: {
					        // Make this a pie chart.
					        renderer: jQuery.jqplot.PieRenderer, 
					        rendererOptions: {
					          // Put data labels on the pie slices.
					          // By default, labels show the percentage of the slice.
					          showDataLabels: true
					        }
			      		}
						,grid: {
							borderColor:'transparent',drawBorder:false,shadowColor:'transparent',
						    drawGridLines: true,        // wether to draw lines across the grid or not.
						    gridLineColor: '#cccccc',   // *Color of the grid lines.
						    background: '#F1F1F1',      // CSS color spec for background color of grid.
						    borderColor: '#cccccc',     // CSS color spec for border around grid.
						    borderWidth: 1.0,           // pixel width of border around grid.
						    shadow: false,               // draw a shadow for grid.
						    shadowAngle: 45,            // angle of the shadow.  Clockwise from x axis.
						    shadowOffset: 1.5,          // offset from the line of the shadow.
						    shadowWidth: 3,             // width of the stroke for the shadow.
						    shadowDepth: 3,             // Number of strokes to make when drawing shadow.
						                                // Each stroke offset by shadowOffset from the last.
						    shadowAlpha: 0.07,          // Opacity of the shadow
						    renderer: $.jqplot.CanvasGridRenderer,  // renderer to use to draw the grid.
						    rendererOptions: {}         // options to pass to the renderer.  Note, the default
						                                // CanvasGridRenderer takes no additional options.
						}
				      	,legend: { show:true, location: 'e'}
			    });
		break;
		case 'D': //donut
  			$.jqplot (div_name, ret.data,{
			    title: chart_title
			    ,seriesDefaults: {
			      // make this a donut chart.
			      renderer:$.jqplot.DonutRenderer,
			      rendererOptions:{
			        // Pies and donuts can start at any arbitrary angle.
			        showDataLabels: true,
			        // By default, data labels show the percentage of the donut/pie.
			        // You can show the data 'value' or data 'label' instead.
			        dataLabels: 'value'
			      }
			    }
				,grid: {
							borderColor:'transparent',drawBorder:false,shadowColor:'transparent',
						    drawGridLines: true,        // wether to draw lines across the grid or not.
						    gridLineColor: '#cccccc',   // *Color of the grid lines.
						    background: '#F1F1F1',      // CSS color spec for background color of grid.
						    borderColor: '#cccccc',     // CSS color spec for border around grid.
						    borderWidth: 1.0,           // pixel width of border around grid.
						    shadow: false,               // draw a shadow for grid.
						    shadowAngle: 45,            // angle of the shadow.  Clockwise from x axis.
						    shadowOffset: 1.5,          // offset from the line of the shadow.
						    shadowWidth: 3,             // width of the stroke for the shadow.
						    shadowDepth: 3,             // Number of strokes to make when drawing shadow.
						                                // Each stroke offset by shadowOffset from the last.
						    shadowAlpha: 0.07,          // Opacity of the shadow
						    renderer: $.jqplot.CanvasGridRenderer,  // renderer to use to draw the grid.
						    rendererOptions: {}         // options to pass to the renderer.  Note, the default
						                                // CanvasGridRenderer takes no additional options.
				}
		      	,legend: { show:true, location: 'e' }
			  });
		break;
		case 'B': //BAR
			    // Can specify a custom tick Array.
			    // Ticks should match up one for each y value (category) in the series.
			    // var ticks = ['May', 'June', 'July', 'August'];
			     
	  			$.jqplot (div_name, ret.data,{
				    title: chart_title
				    // ,stackSeries: true
			        // The "seriesDefaults" option is an options object that will
			        // be applied to all series in the chart.
			        ,seriesDefaults:{
			            renderer:$.jqplot.BarRenderer
				        ,pointLabels: {show: true}
			            // rendererOptions: {fillToZero: true}
			        },
			        // Custom labels for the series are specified with the "label"
			        // option on the series option.  Here a series option object
			        // is specified for each series.
			        series: series,
			        // Show the legend and put it outside the grid, but inside the
			        // plot container, shrinking the grid to accomodate the legend.
			        // A value of "outside" would not shrink the grid and allow
			        // the legend to overflow the container.
			        legend: {
			            show: true,
			            placement: 'outsideGrid'
			        },
			        axes: {
			            // Use a category axis on the x axis and use our custom ticks.
			            xaxis: {
			                renderer: $.jqplot.CategoryAxisRenderer,
			                ticks: ticks
			            },
			            // Pad the y axis just a little so bars can get close to, but
			            // not touch, the grid boundaries.  1.2 is the default padding.
			            // yaxis: {
			                // pad: 1.05,
			                // tickOptions: {formatString: '$%d'}
			            // }
			        },
					grid: {
								borderColor:'transparent',drawBorder:false,shadowColor:'transparent',
							    drawGridLines: true,        // wether to draw lines across the grid or not.
							    gridLineColor: '#cccccc',   // *Color of the grid lines.
							    background: '#F1F1F1',      // CSS color spec for background color of grid.
							    borderColor: '#cccccc',     // CSS color spec for border around grid.
							    borderWidth: 1.0,           // pixel width of border around grid.
							    shadow: false,               // draw a shadow for grid.
							    shadowAngle: 45,            // angle of the shadow.  Clockwise from x axis.
							    shadowOffset: 1.5,          // offset from the line of the shadow.
							    shadowWidth: 3,             // width of the stroke for the shadow.
							    shadowDepth: 3,             // Number of strokes to make when drawing shadow.
							                                // Each stroke offset by shadowOffset from the last.
							    shadowAlpha: 0.07,          // Opacity of the shadow
							    renderer: $.jqplot.CanvasGridRenderer,  // renderer to use to draw the grid.
							    rendererOptions: {}         // options to pass to the renderer.  Note, the default
							                                // CanvasGridRenderer takes no additional options.
					}
			    });
		break;
		default:
	}
	
}